package com.example.hotfix;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.util.Arrays;

import dalvik.system.PathClassLoader;

public class MainActivity extends AppCompatActivity {
    private static String TAG = "Mainactivity";
    private static Context context;
    private static final String PREFS_NAME = "MyPrefs";
    private static final String FIX_KEY = "FIX";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_run = findViewById(R.id.btn_run);
        Button btn_repair = findViewById(R.id.btn_repair);
         context = MainActivity.this;
        SharedPreferences sp = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        if (sp.getBoolean(FIX_KEY,false)){
            btn_repair.setVisibility(View.GONE);
        }
        btn_run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new BugTest().getbug();
            }
        });
        btn_repair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit();
                    editor.putBoolean(FIX_KEY, true).apply();
                    showRestartDialog(MainActivity.this);
                    checkFix(MainActivity.this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public static void checkFix(Context context) {
        try {
            int rawid = R.raw.classes;
            new MyFix().fixDexFile(context, rawid);
        } catch (Exception e) {
            Toast.makeText(context, "修复失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    private void getarrly() {
        try {
            PathClassLoader pathClassLoader = (PathClassLoader) MainActivity.this.getClassLoader();
            Object basePathList = new MyFix().getPathList(pathClassLoader);
            Object baseElements = new MyFix().getDexElements(basePathList);

            System.out.println("原数组为：" + Arrays.toString((Object[]) baseElements));

        }catch (Exception e){
            e.printStackTrace();

        }
    }
    public static void showRestartDialog(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("重启应用");
        builder.setMessage("请重启应用");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                restartApp(context);
            }
        });
        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
    private static void restartApp(Context context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
        getarrly();
        System.out.println("123456789987654321");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

}